#!/usr/bin/env python
import os,sys,time,glob
#import Qfunctions as Q
def readcat(filein,fileout, filter):
   """                                                                                                                                                           
   return:                                                                                                                                                       
   ra,dec,imag,sigi,mag1,magB,magV,gmag,rmag                                                                                                                     
   ra,dec,rmag,sigi,mag1,magB,magV,gmag,imag                                                                                                                     
   """
   ff=open(filein,"r")
   ftmp1=ff.readlines()
   ff.close()
   ff=open(fileout,'w')
   for f0 in ftmp1:
       f0t = f0.strip()
       id  = f0t[0:10]
       ra  = f0t[11:21]
       dec = f0t[23:36]
       mag1 = f0t[36:43]
       magB = f0t[168:175]
       magV = f0t[175:182]
       gmag = f0t[182:189]
       rmag = f0t[189:196]
       imag = f0t[196:203]
       pmra = f0t[97:101]
       pmdc = f0t[103:107]
       if len(imag.strip())==0:
           imag= 99
       sigi= f0t[227:233]
       if len(sigi.strip()) ==0:
          sigi = 99
       if len(magB.strip())==0:
          magB = 99
       if len(magV.strip())==0:
          magV = 99

       if len(gmag.strip())==0:
          gmag = 99

       if len(rmag.strip())==0:
          rmag = 99

       if len(pmra.strip())==0:
          pmra = 0

       if len(pmdc.strip())==0:
          pmdc = 0

       if filter == "r":
            aa= "%11.6f %11.6f %5.2f %5.2f %5.2f %5.2f " \
                "%5.2f %5.2f %5.2f %5.2f %5.2f\n" \
                %(float(ra),float(dec),float(rmag),float(sigi),float(mag1),float(magB),float(magV),
                  float(gmag),float(imag),float(pmra)*0.1,float(pmdc)*0.1)
       elif filter == "g":
           aa = "%11.6f %11.6f %5.2f %5.2f %5.2f %5.2f " \
                "%5.2f %5.2f %5.2f %5.2f %5.2f\n" \
                % (float(ra), float(dec), float(gmag), float(sigi), float(mag1), float(magB), float(magV),
                   float(gmag), float(imag), float(pmra) * 0.1, float(pmdc) * 0.1)
       elif filter =="i":
           aa = "%11.6f %11.6f %5.2f %5.2f %5.2f %5.2f " \
                "%5.2f %5.2f %5.2f %5.2f %5.2f\n" \
                % (float(ra), float(dec), float(imag), float(sigi), float(mag1), float(magB), float(magV),
                   float(gmag), float(imag), float(pmra) * 0.1, float(pmdc) * 0.1)
       else:
           aa = "%11.6f %11.6f %5.2f %5.2f %5.2f %5.2f " \
                "%5.2f %5.2f %5.2f %5.2f %5.2f\n" \
                % (float(ra), float(dec), float(rmag), float(sigi), float(mag1), float(magB), float(magV),
                   float(gmag), float(imag), float(pmra) * 0.1, float(pmdc) * 0.1)           
       ff.write(aa)
   ff.close()
   return 
def ucac4(ra,dec,raw,decw,filename, filter):
#/disk1/opt/catalog/ucac4/access/u4test 222.522092 8.136377 4.0 4.0 /disk1/opt/catalog/ucac4/u4b/
#output is "ucac4.txt"
   aa="/disk1/opt/catalog/ucac4/access/u4test "+str(ra)+" "+str(dec)+" "+str(raw)+" "+str(decw)+" /disk1/opt/catalog/ucac4/u4b/"
   aa_out=os.popen(aa).readlines()
   filein="ucac4.txt"
   fileout=filename
   readcat(filein,fileout, filter)
 #  Q.sortfile(fileout,3,0)
   
if __name__=='__main__':
        ra0=float(sys.argv[1])
        dec0=float(sys.argv[2])
        raw=float(sys.argv[3])
        decw=float(sys.argv[4])
        filename=sys.argv[5]
        filter=sys.argv[6]
        ucac4(ra0,dec0,raw,decw,filename,filter)
